package com.superman.supermarket.client.composite.preorder.coupon;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.widget.pop.BasePopupWindow;
import com.superman.mvpframe.client.widget.toggle.ToggleItem;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent.ToggleSelectEventHandler;
import com.superman.supermarket.client.event.PreorderPageEvent;
import com.superman.supermarket.client.event.PreorderPageEvent.PreorderPageEventType;

/**
 * 优惠券选择模块
 * 
 * @author superman
 * @version 2018年8月19日上午10:33:26
 */
public class CouponChooseModule extends BasePopupWindow {

	interface CouponChooseModuleUiBinder extends UiBinder<Widget, CouponChooseModule> {
	}

	private static CouponChooseModuleUiBinder uiBinder = GWT.create(CouponChooseModuleUiBinder.class);

	private String choosedCouponId;

	@UiField
	CouponChooseList couponchooselist;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月19日 上午10:33:18
	 */
	public CouponChooseModule() {
		this.add(uiBinder.createAndBindUi(this));
		this.setHideOnBackgroundClick(true);

		couponchooselist.addToggleSelectEventHandler(new ToggleSelectEventHandler() {

			@Override
			public void onSelected(ToggleSelectEvent event) {
				ToggleItem toggleItem = event.getToggleItem();
				if (toggleItem instanceof CouponChooseCellModule) {
					CouponChooseCellModule cccm = (CouponChooseCellModule) toggleItem;
					Map<String, Object> data = cccm.getData();
					choosedCouponId = String.valueOf(data.get("COUPON_ID"));

				}
			}
		});
	}

	/**
	 * 设置优惠券列表
	 * 
	 * @author superman
	 * @version 2018年8月19日 上午10:48:18
	 * @param list
	 * @param choosedCouponId
	 */
	public void setCouponList(List<Map<String, Object>> list, String choosedCouponId) {
		couponchooselist.setListData(list, choosedCouponId);
	}

	@UiHandler("submitbtn")
	void settlementByCoupon(ClickEvent event) {
		hide();

		PreorderPageEvent pageEvent = new PreorderPageEvent(choosedCouponId, PreorderPageEventType.SETTLEMENT_BY_COUPON);
		XGinjector.INSTANCE.getEventbusController().fire(pageEvent);
	}

	@UiHandler("notusebtn")
	void settlementByNoCoupon(ClickEvent event) {
		choosedCouponId = "none";
		hide();

		PreorderPageEvent pageEvent = new PreorderPageEvent(choosedCouponId, PreorderPageEventType.SETTLEMENT_BY_COUPON);
		XGinjector.INSTANCE.getEventbusController().fire(pageEvent);
	}
}
